﻿using OpenTK;
using OpenTK.Graphics.OpenGL;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DirectXTest.Src
{
    public class OpenGLTestForm : GameWindow
    {
        public OpenGLTestForm()
            : base(400, 400, OpenTK.Graphics.GraphicsMode.Default, "123", GameWindowFlags.Default, DisplayDevice.Default)
        {
        }

        double[][] ctrlpoints = new double[4][];

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            /*            ctrlpoints[0] = new double[3] { 130, 130, 0 };
                        ctrlpoints[1] = new double[3] { 160, 50, 0 };
                        ctrlpoints[2] = new double[3] { 220, 80, 0 };
                        ctrlpoints[3] = new double[3] { 260, 100, 0 };*/

            /*            GL.Enable(EnableCap.DepthTest);
                        GL.Enable(EnableCap.Texture2D);
                        GL.Enable(EnableCap.Map2Vertex3);
                        GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);*/
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            GL.Viewport(ClientRectangle);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            Title = $"(Vsync: {VSync}) FPS: {1f / e.Time:0}";
            base.OnRenderFrame(e);
            GL.PointSize(10);
            GL.Color3(1.0, 0.0, 0.0);
            GL.Begin(BeginMode.LineStrip);
            GL.Vertex3(-0.8f, 0.1f, 0);
            GL.Vertex3(-0.4f, 0.6f, 0);
            GL.Vertex3(0.2f, 0.8f, 0);
            GL.Vertex3(0.7f, 0.2f, 0);
            GL.End();

            float[] p1 = new float[] { -0.8f, 0.1f };
            float[] p2 = new float[] { -0.4f, 0.6f };
            float[] p3 = new float[] { 0.2f, 0.8f };
            float[] p4 = new float[] { 0.7f, 0.2f };

            double[][] ps = new double[11][];
            for (int j = 0; j < ps.Length; j++)
            {
                ps[j] = new double[2];
            }

            int i = 0;
            for (double t = 0.0; t <= 1.0; t += 0.1)
            {
                double a1 = Math.Pow((1 - t), 3);
                double a2 = Math.Pow((1 - t), 2) * 3 * t;
                double a3 = 3 * t * t * (1 - t);
                double a4 = t * t * t;
                ps[i][0] = a1 * p1[0] + a2 * p2[0] + a3 * p3[0] + a4 * p4[0];
                ps[i][1] = a1 * p1[1] + a2 * p2[1] + a3 * p3[1] + a4 * p4[1];
                i = i + 1;
            }

            GL.Color3(1.0, 1.0, 0.0);
            GL.Begin(BeginMode.LineStrip);
            for (int j = 0; j < 11; j++)
            {
                GL.Vertex3(ps[j]);
            }
            GL.End();

            SwapBuffers();
        }
    }
}